$(window).resize(function(){
  updateUI();
});


$(document).ready(function() {
  if( $("body").attr("id") != "dashboard"){
    updateUI();
  }
});

function updateUI(){
  $('#wrapper').css(
    {
      position:'absolute',
      left: ($(window).width() - $('#wrapper').outerWidth())/2,
      top: ($(window).height() - $('#wrapper').outerHeight())/2
    }
  );
}

var geoc = new google.maps.Geocoder();

function show_spiner(){
    $(".spiner").fadeIn();
  }
  
  function hide_spiner(){
    $(".spiner").fadeOut();
  }
  
  function show_author(userName){
    $("#post").hide();
    $("#author-placeholder").hide();
    $("#post-author").show();
    show_spiner();
    $.get("/rest/user/name/" + userName + ".htm", function(data){
      $("#author-placeholder").empty();
      $("#author-placeholder").append(data);
      hide_spiner();
      $("#author-placeholder").fadeIn();
    });
  }
  
  function hide_author(){
    $("#post-author").hide();
    $("#post").fadeIn();
    hide_spiner(); //XXX
  }
  
  /* ======== Comentarios ========= */
  
  //añadir comentario
  function add_comment(){
    $("#pcontent").hide();
    $("#plist").hide();
    $("#pform").fadeIn();
  }
  
  function reset_comment(){
    $("#plist").hide();
    $("#pform").hide();
    //clear
    $("#comment-text").val("");
    $("#comment-submit").removeAttr("disabled");
    $("#pcontent").fadeIn();
  }
  
  //load post comments
  function show_comments(){
    var pid = $("#pid").val();
    show_spiner();
    $("#pcontent").hide();
    $("#pform").hide();
    $("#pcomments").hide();
    $("#plist").show();
    
    $.get("/rest/comment/post/" + pid + "/limit/10.htm", function(data){
      $("#pcomments").empty();
      $("#pcomments").append(data);
      hide_spiner();
      $("#pcomments").fadeIn();
    });
  }
  
  //submit comment
  function process_comment(){
    show_spiner();
    $("#comment-submit").attr("disabled", "disabled");
    
    $.post("/user/comment", $("#pcomment").serializeArray(), function(data){
      hide_spiner();
      $("#pform").hide();
      reset_comment();
      show_comments();
    });
  }
  
  /* ========= Mensaje ============ */
  
  function compose_message(){
    $("#user-info").hide();
    $("#send-message").hide();
    $("#message-container").fadeIn();
  }
  
  function process_message(){
    show_spiner();
    $("#msubmit").attr("disabled", "disabled");
    $.post("/user/message", $("#mform").serializeArray(), function(data){
      hide_spiner();
      if(data != "OK"){
        alert(data);
      }
      reset_message();
    });
  }
  
  function reset_message(){
    $("#msubmit").removeAttr("disabled");
    $("#mtitle").val("");
    $("#mcontent").val("");
    $("#message-container").hide();
    $("#send-message").show();
    $("#user-info").fadeIn();
  }

/* ============ Interacción =========== */


  function follow(){
    show_spiner();
    $.post("/user/follow", $("#username").serializeArray(), function(data){
      if(data == "OK"){
        $("#follow").hide();
        $("#unfollow").fadeIn();
      } else {
        alert(data);
      }
      hide_spiner();
    });
  }
  
  function unfollow(){
    show_spiner();
    $.post("/user/unfollow", $("#username").serializeArray(), function(data){
      if(data == "OK"){
        $("#unfollow").hide();
        $("#follow").fadeIn();
      } else {
        alert(data);
      }
      hide_spiner();
    });
  }
  
  function fave(){
    show_spiner();
    $.post("/user/fave", $("#pid").serializeArray(), function(data){
      hide_spiner();
      if(data == "OK"){
        $("#fave").hide();
        $("#unfave").fadeIn();
      } else {
        alert(data);
      }
    });
  }
  
  function unfave(){
    show_spiner();
    $.post("/user/unfave", $("#pid").serializeArray(), function(data){
      hide_spiner();
      if(data == "OK"){
        $("#unfave").hide();
        $("#fave").fadeIn();
      } else {
        alert(data);
      }
    });
  }
  
  function edit_post(pid){
    var url = "/user/edit/" + pid;
    $.fancybox.showActivity();

    $.fancybox({
      'autoDimensions'  	: false,
      'width'           	: 350,
      'height'           	: 580,
      'async'             : false,
      'transitionIn'	  	: 'fade',
      'transitionOut'	  	: 'fade',
      'scrolling'         : 'no',
      'overlayShow'       : false,
      'type'              : 'iframe', //para permitir subir archivos
      'href'              : url,
      'onClosed'			    : function(){ showAll(); }
    });
    $.fancybox.hideActivity();
  }
  
  function show_post(pid){
  var url = "/rest/post/" +  pid + ".htm";
  $.fancybox.showActivity();
  $.ajax({
    type	: "GET",
    cache	: false,
    url		: url,
    success: function(data) {
      $.fancybox(data,
        {
          'autoDimensions'  	: false,
          'width'        		  : 850,
          'height'       		  : 450,
          'transitionIn'	  	: 'elastic',
          'transitionOut'	  	: 'elastic',
          'scrolling'         : 'no',
          'overlayShow'       : false,
          'onClosed'			    : function(){ showAll(); }
        }
      );
      degeocode();
    }
  });//ajax
}

function degeocode(){
  $("#loc-caption").text("(identificando ubicación...)");
  var lat = parseFloat( $("#loc-data #loc-lat").val() );
  var lng = parseFloat( $("#loc-data #loc-lng").val() );
  var point = new google.maps.LatLng( lat , lng );
  
  geoc.geocode({'latLng': point}, function(results, status){
		if (status == google.maps.GeocoderStatus.OK){
			if (results[1]){
        $("#loc-caption").text(results[1].formatted_address);
			}
		} else {
			$("#loc-caption").text("Ubicación sin identificar ");
		}
    
    $("#loc-caption").append("<a class='map-link' target='_blank' href='http://maps.google.com/?ll=" + 
        lat +"," +lng +"&z=5'>&nbsp;</a>");
	});
}

